package com.dexilaonian.ui;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.dexilaonian.view.LoadingDialog;

import butterknife.ButterKnife;
public abstract class BaseFragment extends Fragment {
    private FragmentActivity activity;
    private View layoutView;
    private Toast toast;
    private LoadingDialog loadingDialog;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        layoutView = inflater.inflate(getLayoutID(), container, false);
        activity = getActivity();
        loadingDialog = new LoadingDialog(activity);
        loadingDialog.setCancelable(true);
        return layoutView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ButterKnife.bind(layoutView,activity);
        initView(layoutView);
        initData(savedInstanceState);
    }

    protected abstract void initData(Bundle savedInstanceState);

    protected abstract void initView(View layoutView);

    public abstract int getLayoutID();

    public void showToast(String msg) {
        if (null == toast) {
            toast = Toast.makeText(activity, msg, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        } else {
            toast.setText(msg);
        }
        toast.show();
    }

    public void showProgressBar(boolean show) {
        showProgressBar(show,"");
    }

    public void showProgressBar(boolean show, String message) {
        if (loadingDialog == null) {
            return;
        } else {
            if (show) {
                loadingDialog.show();
            } else {
                loadingDialog.hide();
            }
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
